<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script src="../React-js/react.development.js" type="text/javascript"></script>
    <script src="../React-js/react-dom.development.js" type="text/javascript"></script>
    <script src="../React-js/babel.min.js" type="text/javascript"></script>

    <div id="test"></div>
    <script type="text/babel">  
        /*
            生命周期：
                1.初始化阶段：挂载阶段
                    constructor
                    componentWillMount
                    render
                    componentDidMount
        */
        class Count extends React.Component{
            // 构造函数
            constructor(props){
                super(props)
                console.log('constructor--01');
                this.state = {
                    count:0
                }
            }
            // 组件将要挂载
            componentWillMount(){
                console.log('componentWillMount--02');
            }
            // 组件挂载完成
            componentDidMount(){
                console.log('componentDidMount--04');
            }
          
            // 控制组件是否更新“阀门”
            shouldComponentUpdate(){
                console.log('shouldComponentUpdate');
                // 返回true更新，返回false不更新
                return true
            }
            // 组件将要更新
            componentWillUpdate(){
                console.log('componentWillUpdate');
            }
            // 组件更新完成
            componentDidUpdate(){
                console.log('componentDidUpdate');
            }
            // 组件卸载
            componentWillUnmount(){
                console.log('componentWillUnmount--05');
            }
            //强制更新
            refresh = ()=>{
                this.forceUpdate()
            }
            add = ()=>{
                this.setState({count:this.state.count+1})
            }
            destroy = ()=>{
                ReactDOM.unmountComponentAtNode(document.querySelector('#test'))
            }
            // 组件将要更新
            render(){
                console.log('render--03');
                const { count } = this.state
                return (
                    <div>
                        <h1>值:{count}</h1>
                        <button onClick={this.add}>+1</button>
                        <button onClick={this.destroy}>销毁</button>
                        <button onClick={this.refresh}>强制更新</button>
                    </div>
                )
            }
        }
        ReactDOM.render(<Count/>,document.querySelector('#test'))
    </script>
</body>
</html>